Skip to main content

Linear data structures

Linear Data Structures are those where data elements are arranged sequentially, with each element connected to the next and previous one (except for the first and last). This arrangement forms a linear sequence, making it easy to traverse the data in a single run.

Arrays

Arrays store elements in a fixed, contiguous memory allocation and are identifiable by index or key.

Properties

  • Fixed size, making them predictable in memory usage.
  • Efficient at indexing, offering quick access to elements.
  • Poor at resizing dynamically; resizing requires allocating a new array and copying elements.

Linked Lists

Linked Lists are collections of nodes connected via links. Each node contains a data field and one or more links pointing to other nodes.

Types

  • Singly linked list: Nodes have a single link to the next node.
  • Doubly linked list: Nodes have links both to the next and the previous node, enhancing backward traversal.

Properties

  • Dynamic size, allowing the list to grow or shrink as needed.
  • Efficient insertions and deletions, which do not require shifting elements.
  • Inefficient indexing, as accessing an element generally requires traversing from the start of the list.

Stacks

Stacks are collections of elements with operations that add and remove items according to a Last In, First Out (LIFO) principle.

Properties

  • Operates on a LIFO basis, making it ideal for scenarios requiring reversal, such as undo mechanisms.
  • Main operations are push (add) and pop (remove).

Queues

Queues handle elements in a First In, First Out (FIFO) order with operations that add to the back and remove from the front.

Properties

  • Operates on a FIFO basis, ensuring the order of operations is maintained.
  • Primary operations are enqueue (add to the rear) and dequeue (remove from the front).

Common Operations

  • Access: Retrieving an element based on its position.
  • Search: Locating an element using specific criteria.
  • Insertion: Adding an element at a predetermined location.
  • Deletion: Removing an element from the structure.

Applications

  • Arrays: Fundamental for data storage in programs due to their direct element access.
  • Linked Lists: Useful in scenarios like implementing basic data structures (e.g., stacks, queues) or functionalities like undo in text editors.
  • Stacks: Crucial for managing function calls, parsing, recursion, and temporary data storage.
  • Queues: Essential in algorithms like breadth-first search, process scheduling, and managing queues in real-time systems.